package com.xuxingcan.findmyvoice.Activity;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SignUpCallback;
import com.rey.material.app.DialogFragment;
import com.rey.material.widget.EditText;
import com.xuxingcan.findmyvoice.R;
import com.xuxingcan.findmyvoice.Util.BitmapUtil;
import com.xuxingcan.findmyvoice.Util.LeanCloudUtil;

/**
 * Created by Administrator on 2016/3/28.
 */
public class SignUpActivity extends BaseActivity {
    private EditText nickEditText, passwordEditText, repeatPasswordEditText, phoneEditText, keyEditText;
    private TextView sendMsgTextView, confirmTextView;
    private ImageView mUserIcon;
    private Bitmap mUserIconBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        initView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Uri uri = data.getData();
            ContentResolver cr = this.getContentResolver();
            try {
                mUserIconBitmap = BitmapUtil.corpBitmapWithGivenParameter(BitmapFactory.decodeStream(cr.openInputStream(uri)), 500, 500);
                mUserIcon.setImageBitmap(BitmapUtil.getRoundBitmap(mUserIconBitmap));
            } catch (Exception e) {
                Log.e("Exception", e.getMessage(), e);
            } catch (OutOfMemoryError ee) {

            }
        }
    }

    private void initView() {
        mUserIcon = (ImageView) findViewById(R.id.user_icon);
        nickEditText = (EditText) findViewById(R.id.nick_name);
        passwordEditText = (EditText) findViewById(R.id.password);
        repeatPasswordEditText = (EditText) findViewById(R.id.repeat_password);
        confirmTextView = (TextView) findViewById(R.id.confirm);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mUserIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent, 0);
            }
        });
        confirmTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = nickEditText.getText().toString().trim();
                String passwd = passwordEditText.getText().toString().trim();
                String repeatPasswd = repeatPasswordEditText.getText().toString().trim();
                String message = null;
                if (userName == null || userName.length() == 0) {
                    message = "用户名不能为空";
                    showDialog("发生了一点错误", message, "我知道了");
                    return;
                }
                if (passwd == null || passwd.length() == 0 || repeatPasswd == null || repeatPasswd.length() == 0) {
                    message = "密码不能为空";
                    showDialog("发生了一点错误", message, "我知道了");
                    return;
                }
                if (!passwd.equals(repeatPasswd)) {
                    message = "两次输入的密码不相同";
                    showDialog("发生了一点错误", message, "我知道了");
                    return;
                }
                if (passwd.length() < 8) {
                    message = "密码长度不足8位";
                    showDialog("发生了一点错误", message, "我知道了");
                    return;
                }
                if (mUserIconBitmap == null) {
                    message = "请选择一个头像";
                    showDialog("发生了一点错误", message, "我知道了");
                    return;
                }
                AVUser user = new AVUser();
                user.setUsername(userName);
                user.setPassword(passwd);
                AVFile userIconFile = new AVFile("user_icon", LeanCloudUtil.Bitmap2Bytes(mUserIconBitmap));
                user.put(LeanCloudUtil.KEY_USER_ICON, userIconFile);
                user.signUpInBackground(new SignUpCallback() {
                    public void done(AVException e) {
                        if (e == null) {
                            showDialog("恭喜", "注册成功", "我知道了", null, new DialogCallback() {
                                @Override
                                public boolean onPositiveActionClicked(DialogFragment fragment) {
                                    finish();
                                    return false;
                                }

                                @Override
                                public boolean onNegativeActionClicked(DialogFragment fragment) {
                                    return false;
                                }
                            });
                        } else {
                            if (e.getCode() == 202) {
                                showDialog("发生了一点错误", "用户名已经被注册", "我知道了");
                            } else {
                                showDialog("发生了一点错误", e.getMessage(), "我知道了");
                            }
                        }
                    }
                });
            }
        });
    }
}
